﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using MS.Common;
using Prism.Commands;
using Prism.Events;
using Unity;
using Prism.Unity;
using Prism.Ioc;
using Prism.Regions;
using wpf.Triage.Views;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Media;
using wpf.Triage.Behavior;

namespace wpf.Triage
{

    public class NursingViewModel : VmBase, INavigationAware
    {

        private Action<dynamic> _EditCommand;
        /// <summary>
        /// 编辑回调
        /// </summary>
        public Action<dynamic> EditCommand
        {
            get => _EditCommand;
            set
            {
                _EditCommand = value;
                RaisePropertyChanged(nameof(EditCommand));
            }
        }
        Canvas mainCanvas;
        Rectangle rectangle = new Rectangle();
        public void Init(Canvas canvas)
        {
            mainCanvas = canvas;
            mainCanvas.Children.Add(rectangle);
            SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 140, 0));
            rectangle.Stroke = (System.Windows.Media.Brush)mybtn1_Brush; //Brush.Red;
            rectangle.StrokeThickness = 1;
            rectangle.Width = 100;
            rectangle.Height = 100;
            rectangle.SetValue(Canvas.TopProperty, 10.0);
            rectangle.SetValue(Canvas.LeftProperty, 10.0);
            DragInCanvasBehavior drag = new DragInCanvasBehavior();
            System.Windows.Interactivity.Interaction.GetBehaviors(rectangle).Add(drag);
        }

        public DelegateCommand<object> TriageNavCommand { get; private set; }
        public DelegateCommand<object> PatListCommand { get; private set; }

        public NursingViewModel(IContainerExtension container, IEventAggregator eventAggregator, IRegionManager regionManager) : base(container, eventAggregator, regionManager)
        {
            EditCommand = new Action<dynamic>(OnEdit);
            this.TriageNavCommand = new DelegateCommand<object>(OnTriageNavCommand);
            this.PatListCommand = new DelegateCommand<object>(OnPatListCommand);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dy"></param>
        public void OnEdit(dynamic dy)
        {
            if (dy is DrawRect rect)
            {
                if (rect!=null)
                {
                    rectangle.Width = rect.Width;
                    rectangle.Height = rect.Height;
                    rectangle.SetValue(Canvas.TopProperty, rect.Y);
                    rectangle.SetValue(Canvas.LeftProperty, rect.X);
                }
            }
        }
        private void OnTriageNavCommand(object obj)
        {
            string navStr = obj.ToString();
            var uri = new Uri(navStr, UriKind.Relative);
            regionManager.RequestNavigate(RegionName.TriageRegionName, uri);
        }
        private void OnPatListCommand(object obj)
        {
           
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {   //第一次进来进入分诊
            var uri = new Uri(nameof(TriageRegisterView), UriKind.Relative);
            regionManager.RequestNavigate(RegionName.TriageRegionName, uri);
        }
    }
}
